package cn.com.haopy.yue.ai.admin.web;

import cn.com.haopy.yue.ai.admin.annotation.RequiresPermissionsDesc;
import cn.com.haopy.yue.ai.core.system.SystemConfig;
import cn.com.haopy.yue.ai.core.utils.JacksonUtil;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.db.service.hos.HOSSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/config")
@Validated
@Slf4j
@RequiredArgsConstructor
@Api(tags = "配置接口相关")
public class AdminConfigController {

    private final HOSSystemService systemService;


    @RequiresPermissions("admin:config:system:detail")
    @RequiresPermissionsDesc(menu = {"配置管理", "系统配置"}, button = "详情")
    @GetMapping("/systemConfigDetail")
    @ApiOperation("系统配置详情")
    public Object systemConfigDetail() {
        Map<String, String> data = systemService.getConfigByKey(new String[]{
                SystemConfig.USER_RISK_WX_PRIVACY,
                SystemConfig.USER_RISK_WX_PROTOCOL,
                SystemConfig.VIP_CHARGE_MONTH,
                SystemConfig.VIP_CHARGE_MONEY,
        });
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:config:system:updateConfigs")
    @RequiresPermissionsDesc(menu = {"配置管理", "系统配置"}, button = "编辑")
    @PostMapping("/systemConfigUpdate")
    @ApiOperation("系统配置修改")
    public Object systemConfigUpdate(@RequestBody String body) {
        Map<String, String> data = JacksonUtil.toMap(body);
        systemService.updateConfig(data);
        SystemConfig.updateConfigs(data);
        return ResponseUtil.ok();
    }


}
